gh-pages set-up

Some of you have already been working in blogdown. But there are other ways that you can make your RMarkdown materials available for others to see. We’ll create a repo that contains .Rmd and knitted .html files that we can share as web pages!

Note: The gh-pages section of this lab is inspired by materials from Dr. Julia Lowndes (https://jules32.github.io/).

Julia has written a post on how to make website with RMarkdown and GitHub here: https://jules32.github.io/rmarkdown-website-tutorial/

Follow along with these steps to make your gh-pages branch:

In that .Rmd:

0. Attach packages

# For general stuff:
library(tidyverse)
library(janitor)
library(lubridate)
library(here)
library(paletteer)

# For ts stuff: 
library(tsibble)
library(fable)
library(fabletools)
library(feasts)
library(forecast)

# For spatial stuff: 
library(sf)
library(tmap)
library(mapview)

Monthly US energy consumption (renewables)

1. Get the data

We’ll explore, then forecast, US energy consumption and production by renewables source. Get the data from renewables_cons_prod.csv:

us_renew <- read_csv(here("data", "renewables_cons_prod.csv")) %>% 
  clean_names()

Explore the data frame:

# View(us_renew)
# names(us_renew)
# unique(us_renew$description)

We’ll focus on consumption data.

Clean up data

  • Convert description to all lowercase
  • Only keep observations for “consumption”
  • Remove any “total” observations
renew_clean <- us_renew %>% 
  mutate(description = str_to_lower(description)) %>% 
  filter(str_detect(description, pattern = "consumption")) %>% 
  filter(!str_detect(description, pattern = "total"))

Convert yyyymm column to date with lubridate

renew_date <- renew_clean %>% 
  mutate(yr_mo_day = lubridate::parse_date_time(yyyymm, "ym")) %>% 
  mutate(month_sep = yearmonth(yr_mo_day)) %>% #coerce to tsibble `yearmonth` format
  mutate(value = as.numeric(value)) %>% 
  drop_na(month_sep, value)

# Want to parse the year and month? We may use this later...
renew_parsed <-renew_date %>% 
  mutate(month = month(yr_mo_day, label = TRUE)) %>% 
  mutate(year = year(yr_mo_day))

Make a ggplot

Make, then save, the baseline plot as renew_gg:

renew_gg <- ggplot(data = renew_date, aes(x = month_sep, y = value, group = description)) +
  geom_line(aes(color = description)) +
  theme_minimal() +
  scale_y_continuous(limits = c(0, 350))

renew_gg

Now try updating your color palette using options from paletteer. Use View(palettes_d_names) to see all of the discrete scale options. We’ll want a palette with at least 7 colors (length >= 7). Find a name of a palette that you like, then update your graph by adding scale_color_paletteer_d("package::palette"). Like, if I want to use the calecopal::figmtn palette, I’d add:

renew_gg + scale_color_paletteer_d("calecopal::figmtn")

Try some out!

renew_gg +
  scale_color_paletteer_d("calecopal::figmtn")

Have some fun trying out different color palettes.

Coerce to a tsibble:

renew_ts <- as_tsibble(renew_parsed, key = description, index = month_sep)

Look at the data in a few different ways:

renew_ts %>% autoplot(value)

renew_ts %>% gg_subseries(value)

renew_ts %>% gg_season(value)

# What if gg_season() didn't work? Well we can make this with ggplot anyway!
# Remember our other parsed version (renew parsed):

ggplot(data = renew_parsed, aes(x = month, y = value, group = year)) +
  geom_line(aes(color = year)) +
  facet_wrap(~ description, 
             ncol = 1, 
             scales = "free", 
             strip.position = "right")

Get just the hydroelectric energy consumption data:

hydro_ts <- renew_ts %>% 
  filter(description == "hydroelectric power consumption")

# Explore: 
hydro_ts %>% autoplot(value)

hydro_ts %>% gg_subseries(value)

hydro_ts %>% gg_season(value)

# OK, what if gg_season() doesn't work?
# It's just a function that uses ggplot() to do things we already know how to do in ggplot()!
ggplot(hydro_ts, aes(x = month, y = value, group = year)) +
  geom_line(aes(color = year))

Calculate summary data by time using index_by()

What if we want to calculate consumption by quarter? We’ll use index_by() to tell R which “windows” to calculate a value with in.

Quarterly:

hydro_quarterly <- hydro_ts %>% 
  index_by(year_qu = ~ yearquarter(.)) %>% # monthly aggregates
  summarise(
    avg_consumption = mean(value)
  )

head(hydro_quarterly)
## # A tsibble: 6 x 2 [1Q]
##   year_qu avg_consumption
##     <qtr>           <dbl>
## 1 1973 Q1            261.
## 2 1973 Q2            255.
## 3 1973 Q3            212.
## 4 1973 Q4            225.
## 5 1974 Q1            292.
## 6 1974 Q2            290.

Or annually:

hydro_annual <- hydro_ts %>% 
  index_by(annual = ~year(.)) %>% 
  summarize(
    avg_consumption = mean(value)
  )

ggplot(data = hydro_annual, aes(x = annual, y = avg_consumption)) +
  geom_line(color = "darkorange") +
  geom_smooth(color = "purple",
              size = 0.2,
              linetype = "dashed",
              fill = "purple",
              alpha = 0.2) +
  theme_minimal()

And if you have higher interval data (e.g. hourly), then you can calculate summaries by week, month, etc. using functions from tsibble like:

  • yearweek
  • yearmonth

Decompose the hydro consumption ts data

First, let’s check the decomposition (STL):

# Find STL decomposition
dcmp <- hydro_ts %>%
  model(STL(value ~ season(window = 5)))

# View the components
# components(dcmp)

# Visualize the decomposed components
components(dcmp) %>% autoplot() +
  theme_minimal()

# Let's check out the residuals:
hist(components(dcmp)$remainder)

Explore the ACF

hydro_ts %>% 
  ACF(value) %>% 
  autoplot()

We see peaks at 12 months: annual-difference similarities in consumption.

Forecast future hydro power consumption

hydro_model <- hydro_ts %>%
  model(
    arima = ARIMA(value)
  ) %>%
  fabletools::forecast(h = "2 years")

hydro_model %>% autoplot(filter(hydro_ts, year(month_sep) > 2010), level = NULL)

Map-of-the-day

A world map with bubbles!

# Get spatial data: 
world <- read_sf(dsn = here("data","TM_WORLD_BORDERS_SIMPL-0.3-1"), layer = "TM_WORLD_BORDERS_SIMPL-0.3") %>% clean_names()

# Quick & easy option to see those polygons (also for points, lines!)
mapview(world)
# ggplot (static)
world_base <- ggplot(data = world) +
  geom_sf(aes(fill = pop2005),
          color = NA) + 
  scale_fill_paletteer_c("viridis::viridis") +
  theme_minimal()

world_base

# Let's crop it: 
world_base +
  coord_sf(xlim = c(-20, 50), ylim = c(-40, 40), expand = FALSE)

Making this a web page:

Since you are working in a gh-pages branch, this can be a webpage!

End Lab Week 5